from abc import ABC, abstractmethod


# Implementor 接口（抽象基类）
class Implementor(ABC):
    @abstractmethod
    def operation_impl(self):
        pass


# ConcreteImplementorA 类，实现了 Implementor 接口
class ConcreteImplementorA(Implementor):
    def operation_impl(self):
        return "ConcreteImplementorA Operation"


# ConcreteImplementorB 类，实现了 Implementor 接口
class ConcreteImplementorB(Implementor):
    def operation_impl(self):
        return "ConcreteImplementorB Operation"


# Abstraction 类，持有一个 Implementor 的引用
class Abstraction(ABC):
    def __init__(self, implementor: Implementor):
        self._implementor = implementor

    @abstractmethod
    def operation(self):
        pass


# RefinedAbstraction 类，是 Abstraction 的一个具体子类
class RefinedAbstraction(Abstraction):
    def operation(self):
        # 调用 Implementor 的具体实现
        return f"RefinedAbstraction({self._implementor.operation_impl()})"


# Client 类，使用 Abstraction 接口
class Client:
    def __init__(self, abstraction: Abstraction):
        self._abstraction = abstraction

    def perform_operation(self):
        print(self._abstraction.operation())


# 使用示例
if __name__ == "__main__":
    # 创建 Implementor 的具体实现
    implementor_a = ConcreteImplementorA()
    implementor_b = ConcreteImplementorB()

    # 创建 Abstraction 的具体子类实例，并传入 Implementor 的实现
    abstraction_a = RefinedAbstraction(implementor_a)
    abstraction_b = RefinedAbstraction(implementor_b)

    # 创建 Client 实例并执行操作
    client_a = Client(abstraction_a)
    client_b = Client(abstraction_b)

    client_a.perform_operation()  # 输出: RefinedAbstraction(ConcreteImplementorA Operation)
    client_b.perform_operation()  # 输出: RefinedAbstraction(ConcreteImplementorB Operation)